using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

using Microsoft.Xna;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

using FarseerGames.FarseerPhysics; 
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;

using FarseerGames.FarseerPhysicsDemos.ScreenSystem;
using FarseerGames.FarseerPhysicsDemos.DrawingSystem;



namespace FarseerGames.FarseerPhysicsDemos.Demos.Demo2 {
    public class Demo2Screen : GameScreen{
        //declare variables
        LineBrush lineBrush = new LineBrush(1, Color.Black); //used to draw spring on mouse grab
        ContentManager contentManager;
        bool debugViewEnabled = false;
        float timer = 0f;
        float interval = 1000.0f;
        
        bool timeup = false;
        bool clicked = false;

        Texture2D rectangleTexture;
        Texture2D circleTexture, floorTexture, leftWallTexture, rightWallTexture, topWallTexture, circleTexture2, circleTexture3;
        Texture2D holdTexture, holdTexture2;
        Texture2D armTexture;
        Texture2D rightShoulderTexture, rightForearmTexture, leftShoulderTexture, leftForearmTexture;
        Texture2D rightThighTexture, rightCalfTexture, leftThighTexture, leftCalfTexture;
        Texture2D rightHandTexture, leftHandTexture;
        Texture2D headTexture;
        
        Vector2 rectangleOrigin;
        Vector2 circleOrigin, circleOrigin2, circleOrigin3;
        Vector2 holdOrigin, holdOrigin2;
        Vector2 floorOrigin, leftWallOrigin, rightWallOrigin, topWallOrigin;
        Vector2 armOrigin;
        Vector2 rightShoulderOrigin, rightForearmOrigin, leftShoulderOrigin, leftForearmOrigin;
        Vector2 rightThighOrigin, rightCalfOrigin, leftThighOrigin, leftCalfOrigin;
        Vector2 headOrigin;
        Vector2 rightHandOrigin, leftHandOrigin;

        float anAngle = 1f;

        PhysicsSimulator physicsSimulator;
        PhysicsSimulatorView physicsSimulatorView;
        
        Body rectangleBody;
        Body circleBody, circleBody2, circleBody3;
        Body holdBody, holdBody2;
        Body floorBody, rightWallBody, leftWallBody, topWallBody;
        Body armBody;
        Body rightShoulderBody, rightForearmBody, leftShoulderBody, leftForearmBody;
        Body rightThighBody, rightCalfBody, leftThighBody, leftCalfBody;
        Body headBody;
        Body rightHandBody, leftHandBody;
        PinJoint testJoint;

        PinJoint rightShoulderJoint, rightElbowJoint, leftShoulderJoint, leftElbowJoint;
        PinJoint rightHipJoint, rightKneeJoint, leftHipJoint, leftKneeJoint;
        PinJoint rightWristJoint,leftWristJoint;
        PinJoint neckJoint;
        PinJoint pj = null;

        AngleLimitJoint rightShoulderAngleLimitJoint, leftShoulderAngleLimitJoint, leftThighAngleLimitJoint, rightThighAngleLimitJoint;
        AngleLimitJoint leftKneeAngleLimitJoint, rightKneeAngleLimitJoint;
        AngleLimitJoint rightWristAngleLimitJoint, leftWristAngleLimitJoint;
        AngleLimitJoint neckAngleLimitJoint;

        BruteForceCollider bfc;
        Geom rectangleGeom;
        Geom circleGeom, circleGeom2, circleGeom3;
        Geom holdGeom, holdGeom2;
        Geom floorGeom, rightWallGeom, leftWallGeom, topWallGeom;
        Geom armGeom;
        Geom rightShoulderGeom, rightForearmGeom, leftShoulderGeom, leftForearmGeom;
        Geom rightThighGeom, rightCalfGeom, leftThighGeom, leftCalfGeom;
        Geom headGeom;
        Geom rightHandGeom, leftHandGeom;
        Contact contact;

        public Demo2Screen() {
            physicsSimulator = new PhysicsSimulator(new Vector2(0, 1200));
            physicsSimulatorView = new PhysicsSimulatorView(physicsSimulator);
        }

        private bool CollisionTest(Geom geometry1, Geom geometry2, ContactList contactList)
        {
            if ((geometry1.Body.IsHand) && (geometry2.Body.IsHold))
            {
                geometry1.Body.IsStatic = true;
                return false;
            }
            else if ((geometry1.Body.IsHold) && (geometry2.Body.IsHand))
            {
                geometry2.Body.IsStatic = true;
                return false;
            }
            else return false; 
        }

        public override void LoadContent()
        {
            if (contentManager == null) contentManager = new ContentManager(ScreenManager.Game.Services);
            lineBrush.Load(ScreenManager.GraphicsDevice);
            physicsSimulatorView.LoadContent(ScreenManager.GraphicsDevice, contentManager);

            rectangleTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 60, 170, Color.Gold, Color.Black);
            rectangleOrigin = new Vector2(rectangleTexture.Width / 2f, rectangleTexture.Height / 2f);

            rightShoulderTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 100, 15, Color.White, Color.Black);
            rightShoulderOrigin = new Vector2(rightShoulderTexture.Width / 2f, rightShoulderTexture.Height / 2f);

            leftShoulderTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 100, 15, Color.White, Color.Black);
            leftShoulderOrigin = new Vector2(leftShoulderTexture.Width / 2f, leftShoulderTexture.Height / 2f);

            rightForearmTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 90, 15, Color.White, Color.Black);
            rightForearmOrigin = new Vector2(rightForearmTexture.Width / 2f, rightForearmTexture.Height / 2f);

            leftForearmTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 50, 15, Color.White, Color.Black);
            leftForearmOrigin = new Vector2(leftForearmTexture.Width / 2f, leftForearmTexture.Height / 2f);

            rightThighTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 25, 70, Color.White, Color.Black);
            rightThighOrigin = new Vector2(rightThighTexture.Width / 2f, rightThighTexture.Height / 2f);

            leftThighTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 25, 70, Color.White, Color.Black);
            leftThighOrigin = new Vector2(leftThighTexture.Width / 2f, leftThighTexture.Height / 2f);

            rightCalfTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 25, 70, Color.White, Color.Black);
            rightCalfOrigin = new Vector2(rightCalfTexture.Width / 2f, rightCalfTexture.Height / 2f);

            leftCalfTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 25, 70, Color.White, Color.Black);
            leftCalfOrigin = new Vector2(leftCalfTexture.Width / 2f, leftCalfTexture.Height / 2f);

            headTexture = DrawingHelper.CreateCircleTexture(ScreenManager.GraphicsDevice, 30, Color.Gold, Color.Black);
            headOrigin = new Vector2(headTexture.Width / 2f, headTexture.Height / 2f);

            rightHandTexture = DrawingHelper.CreateCircleTexture(ScreenManager.GraphicsDevice, 10, Color.Gold, Color.Black);
            rightHandOrigin = new Vector2(rightHandTexture.Width / 2f, rightHandTexture.Width / 2f);
            
            leftHandTexture = DrawingHelper.CreateCircleTexture(ScreenManager.GraphicsDevice, 10, Color.Gold, Color.Black);
            leftHandOrigin = new Vector2(leftHandTexture.Width / 2f, leftHandTexture.Width / 2f);
            
            /*
            circleTexture = DrawingHelper.CreateCircleTexture(ScreenManager.GraphicsDevice, 64, Color.White, Color.Black);
            circleOrigin = new Vector2(circleTexture.Width / 2f, circleTexture.Height / 2f);
            */
            holdTexture = DrawingHelper.CreateCircleTexture(ScreenManager.GraphicsDevice, 10, Color.White, Color.Black);
            holdOrigin = new Vector2(holdTexture.Width / 2f, holdTexture.Height / 2f);

            holdTexture2 = DrawingHelper.CreateCircleTexture(ScreenManager.GraphicsDevice, 10, Color.White, Color.Black);
            holdOrigin2 = new Vector2(holdTexture2.Width / 2f, holdTexture2.Height / 2f);

            circleTexture2 = DrawingHelper.CreateCircleTexture(ScreenManager.GraphicsDevice, 10, Color.White, Color.Black);
            circleOrigin2 = new Vector2(circleTexture2.Width / 2f, circleTexture2.Height / 2f);
            
            circleTexture3 = DrawingHelper.CreateCircleTexture(ScreenManager.GraphicsDevice, 10, Color.White, Color.Black);
            circleOrigin3 = new Vector2(circleTexture3.Width / 2f, circleTexture3.Height / 2f);

            floorTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 2024, 20, Color.White, Color.Black);
            floorOrigin = new Vector2(floorTexture.Width / 2f, floorTexture.Height / 2f);
            
            leftWallTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 20,768, Color.White, Color.Black);
            leftWallOrigin = new Vector2(leftWallTexture.Width / 2f, leftWallTexture.Height / 2f);
                        rightWallTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 20, 768, Color.White, Color.Black);
            rightWallOrigin = new Vector2(rightWallTexture.Width / 2f, rightWallTexture.Height / 2f);

            topWallTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 2024, 20, Color.White, Color.Black);
            topWallOrigin = new Vector2(topWallTexture.Width / 2f, topWallTexture.Height / 2f);

            rectangleBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 60, 1000, 1);
            rectangleBody.Position = new Vector2(257, 200);
            rectangleBody.IsStatic=false;
            rectangleGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, rectangleBody, 60, 170);
            rectangleBody.LinearDragCoefficient = 50f;
            //rectangleGeom.Collision += CollisionTest;
            
            /*
            circleBody = BodyFactory.Instance.CreateCircleBody(physicsSimulator, 64, 1);//fix 
            circleBody.Position = new Vector2(600, 600);
            circleBody.IsStatic = true;
            circleGeom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, circleBody, 64, 20);
            */
            circleBody2 = BodyFactory.Instance.CreateCircleBody(physicsSimulator, 10, 1);//fix 
            circleBody2.Position = new Vector2(95, 400);
            circleBody2.IsStatic = true;
            circleBody2.IsHold = true;
            circleGeom2 = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, circleBody2, 10, 20);
            circleGeom2.Collision += CollisionTest;           

            circleBody3 = BodyFactory.Instance.CreateCircleBody(physicsSimulator, 10, 1);//fix 
            circleBody3.Position = new Vector2(510, 200);
            circleBody3.IsStatic = true;
            circleBody3.IsHold = true;
            circleGeom3 = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, circleBody3, 3, 20);
            circleGeom3.Collision += CollisionTest;

            holdBody = BodyFactory.Instance.CreateCircleBody(physicsSimulator, 10, 1);
            holdBody.Position = new Vector2(200, 200);
            holdBody.IsStatic = true;
            holdBody.IsHold = true;
            holdGeom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, holdBody, 10, 20);
            holdGeom.Collision += CollisionTest;

            holdBody2 = BodyFactory.Instance.CreateCircleBody(physicsSimulator, 10, 1);
            holdBody2.Position = new Vector2(700, 200);
            holdBody2.IsStatic = true;
            holdBody2.IsHold = true;
            holdGeom2 = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, holdBody2, 10, 20);
            holdGeom2.Collision += CollisionTest;

            rightHandBody = BodyFactory.Instance.CreateCircleBody(physicsSimulator, 10, 1);
            rightHandBody.IsHand = true;
            rightHandBody.Position = new Vector2(505, 125);
            rightHandGeom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, rightHandBody, 10, 20);
            //rightHandGeom.Collision += CollisionTest;
            
            leftHandBody = BodyFactory.Instance.CreateCircleBody(physicsSimulator, 10, 1);
            leftHandBody.IsHand = true;
            leftHandBody.Position = new Vector2(60, 120);
            leftHandGeom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, leftHandBody, 10, 20);
            //leftHandGeom.Collision += CollisionTest;            

            floorBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 2024, 20, 1);
            floorBody.Position = new Vector2(0, 765);
            floorBody.IsStatic = true;
            floorGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, floorBody, 2024, 20);

            leftWallBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 20, 768, 1);
            leftWallBody.Position = new Vector2(0, 375);
            leftWallBody.IsStatic = true;
            leftWallGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, leftWallBody, 20,768);

            rightWallBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 20, 768, 1);
            rightWallBody.Position = new Vector2(1024, 375);
            rightWallBody.IsStatic = true;
            rightWallGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, rightWallBody, 20, 768);

            topWallBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 2024, 20, 1);
            topWallBody.Position = new Vector2(0, 0);
            topWallBody.IsStatic = true;
            topWallGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, topWallBody, 2024, 20);
                  
            rightShoulderBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 100, 15, 10);
            rightShoulderBody.IsStatic = false;
            rightShoulderBody.Position = new Vector2(345, 120);
            rightShoulderGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, rightShoulderBody, 100, 15);
            rightShoulderAngleLimitJoint = JointFactory.Instance.CreateAngleLimitJoint(physicsSimulator, rectangleBody, rightShoulderBody, -1f,2f);
            physicsSimulator.Add(rightShoulderAngleLimitJoint);
            rightShoulderBody.LinearDragCoefficient = 1f;

            leftShoulderBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 100, 15, 10);
            leftShoulderBody.Position = new Vector2(170, 120);
            leftShoulderBody.IsStatic = false;
            leftShoulderGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, leftShoulderBody, 100, 15);
            leftShoulderAngleLimitJoint = JointFactory.Instance.CreateAngleLimitJoint(physicsSimulator, rectangleBody, leftShoulderBody, -2f, 1f);
            physicsSimulator.Add(leftShoulderAngleLimitJoint);
            leftShoulderBody.LinearDragCoefficient = 1f;

            rightForearmBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 90, 15, 10);
            rightForearmBody.IsStatic = false;
            rightForearmBody.Position = new Vector2(450, 120);
            rightForearmBody.LinearDragCoefficient = 1f; 
            rightForearmGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, rightForearmBody, 90, 15);

            leftForearmBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 50, 15, 10);
            leftForearmBody.Position = new Vector2(100, 120);
            leftForearmGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, leftForearmBody, 50, 15);
            leftForearmBody.IsStatic = false;
            leftForearmBody.LinearDragCoefficient = 1f;

            rightThighBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 25, 70, 1);
            rightThighBody.Position = new Vector2(285, 330);
            rightThighGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, rightThighBody, 25, 70);
            rightThighBody.IsStatic = false;
            rightThighAngleLimitJoint = JointFactory.Instance.CreateAngleLimitJoint(physicsSimulator, rectangleBody, rightThighBody, -.5f, .5f);
            rightThighBody.LinearDragCoefficient = 1f;
            physicsSimulator.Add(rightThighAngleLimitJoint);

            leftThighBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 25, 70, 1);
            leftThighBody.Position = new Vector2(225, 330);
            leftThighBody.IsStatic = false;
            leftThighBody.LinearDragCoefficient = 1f;
            leftThighGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, leftThighBody, 25, 70);
            leftThighAngleLimitJoint = JointFactory.Instance.CreateAngleLimitJoint(physicsSimulator, rectangleBody, leftThighBody, -.5f, .5f);
            physicsSimulator.Add(leftThighAngleLimitJoint);

            rightCalfBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 25, 70, 1);
            rightCalfBody.Position = new Vector2(285, 410);
            rightCalfBody.IsStatic = false;
            rightCalfBody.LinearDragCoefficient = 1f;
            rightCalfGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, rightCalfBody, 25, 70);
            rightKneeAngleLimitJoint = JointFactory.Instance.CreateAngleLimitJoint(physicsSimulator, rightThighBody, rightCalfBody, -.3f, .3f);
            physicsSimulator.Add(rightKneeAngleLimitJoint);

            leftCalfBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 25, 70, 1);
            leftCalfBody.Position = new Vector2(225, 410);
            leftCalfBody.IsStatic = false;
            leftCalfBody.LinearDragCoefficient = 1f;
            leftCalfGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, leftCalfBody, 25, 70);
            leftKneeAngleLimitJoint = JointFactory.Instance.CreateAngleLimitJoint(physicsSimulator, leftThighBody, leftCalfBody, -.3f, .3f);
            physicsSimulator.Add(leftKneeAngleLimitJoint);

            headBody = BodyFactory.Instance.CreateCircleBody(physicsSimulator, 30, 5);
            headBody.IsStatic = false;
            headBody.LinearDragCoefficient = 1f;
            headBody.Position = new Vector2(257, 80);
            headGeom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, headBody, 20,5);

            rightShoulderJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rectangleBody, new Vector2(rectangleTexture.Width/2f,-rectangleTexture.Height/2f), rightShoulderBody, new Vector2(-rightShoulderTexture.Width/2f,0));
            leftShoulderJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rectangleBody, new Vector2(-rectangleTexture.Width / 2f, -rectangleTexture.Height/2f), leftShoulderBody, new Vector2(leftShoulderTexture.Width / 2f, 0));

            rightElbowJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rightShoulderBody, new Vector2(rightShoulderTexture.Width/2f,0),rightForearmBody, new Vector2(-rightForearmTexture.Width/2f,0));
            leftElbowJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, leftShoulderBody, new Vector2(-leftShoulderTexture.Width/2f,0), leftForearmBody, new Vector2(leftForearmTexture.Width/2f,0));

            rightHipJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rectangleBody, new Vector2(rectangleTexture.Width / 2f, rectangleTexture.Height / 2f), rightThighBody, new Vector2(0, -rightThighTexture.Height / 2f));
            leftHipJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rectangleBody, new Vector2(-rectangleTexture.Width / 2f, rectangleTexture.Height / 2f), leftThighBody, new Vector2(0, -leftThighTexture.Height / 2f));

            rightKneeJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rightThighBody, new Vector2(0, rightThighTexture.Height / 2f), rightCalfBody, new Vector2(0, -rightCalfTexture.Height / 2f));
            leftKneeJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, leftThighBody, new Vector2(0, leftThighTexture.Height / 2f), leftCalfBody, new Vector2(0, -leftCalfTexture.Height / 2f));
            
            neckJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rectangleBody, new Vector2(0, -rectangleTexture.Height / 2f), headBody, new Vector2(0, headTexture.Height / 2f));
            neckAngleLimitJoint = JointFactory.Instance.CreateAngleLimitJoint(physicsSimulator, rectangleBody, headBody, -.1f, .1f);
            
            rightWristJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rightForearmBody, new Vector2(rightForearmTexture.Width / 2f, 0), rightHandBody, new Vector2(-rightHandTexture.Width / 2f, 0));
            rightWristAngleLimitJoint = JointFactory.Instance.CreateAngleLimitJoint(physicsSimulator, rightForearmBody, rightHandBody, -2f, 2f);

            leftWristJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, leftForearmBody, new Vector2(-leftForearmTexture.Width / 2f, 0), leftHandBody, new Vector2(leftHandTexture.Width / 2f, 0));
            leftWristAngleLimitJoint = JointFactory.Instance.CreateAngleLimitJoint(physicsSimulator, leftForearmBody, leftHandBody, -2f, 2f);
           }

        public override void UnloadContent(){
            contentManager.Unload();
            physicsSimulator.Clear();
        }

        public bool updatedOnce = false;
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) {
            if (IsActive) {
                physicsSimulator.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);
                updatedOnce = true;
                timer = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }                         
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);            
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime) {
            if (!updatedOnce) { return; }
            ScreenManager.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            //ScreenManager.SpriteBatch.Draw(circleTexture, circleGeom.Position, null, Color.White, circleGeom.Rotation, circleOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(circleTexture2, circleGeom2.Position, null, Color.White, circleGeom2.Rotation, circleOrigin2, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(circleTexture3, circleGeom3.Position, null, Color.White, circleGeom3.Rotation, circleOrigin3, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(holdTexture, holdGeom.Position, null, Color.White, holdGeom.Rotation, holdOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(holdTexture2, holdGeom2.Position, null, Color.White, holdGeom2.Rotation, holdOrigin2, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(rectangleTexture, rectangleGeom.Position, null, Color.White, rectangleGeom.Rotation, rectangleOrigin , 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(floorTexture, floorGeom.Position, null, Color.White, floorGeom.Rotation, floorOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(leftWallTexture, leftWallGeom.Position, null, Color.White, leftWallGeom.Rotation, leftWallOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(rightWallTexture, rightWallGeom.Position, null, Color.White, rightWallGeom.Rotation, rightWallOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(rightShoulderTexture, rightShoulderGeom.Position, null, Color.White, rightShoulderGeom.Rotation, rightShoulderOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(leftShoulderTexture, leftShoulderGeom.Position, null, Color.White, leftShoulderGeom.Rotation, leftShoulderOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(rightForearmTexture, rightForearmGeom.Position, null, Color.White, rightForearmGeom.Rotation, rightForearmOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(leftForearmTexture, leftForearmGeom.Position, null, Color.White, leftForearmGeom.Rotation, leftForearmOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(rightThighTexture, rightThighGeom.Position, null, Color.White, rightThighGeom.Rotation, rightThighOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(leftThighTexture, leftThighGeom.Position, null, Color.White, leftThighGeom.Rotation, leftThighOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(rightCalfTexture, rightCalfGeom.Position, null, Color.White, rightCalfGeom.Rotation, rightCalfOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(leftCalfTexture, leftCalfGeom.Position, null, Color.White, leftCalfGeom.Rotation, leftCalfOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(topWallTexture, topWallGeom.Position, null, Color.White, topWallGeom.Rotation, topWallOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(headTexture, headGeom.Position, null, Color.White, headGeom.Rotation, headOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(rightHandTexture, rightHandGeom.Position, null, Color.White, rightHandGeom.Rotation, rightHandOrigin, 1, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(leftHandTexture, leftHandGeom.Position, null, Color.White, leftHandGeom.Rotation, leftHandOrigin, 1, SpriteEffects.None, 0);

            if (mousePickSpring != null) { 
                lineBrush.Draw(ScreenManager.SpriteBatch, mousePickSpring.Body.GetWorldPosition(mousePickSpring.BodyAttachPoint), mousePickSpring.WorldAttachPoint); 
            }
            if (debugViewEnabled) {
                physicsSimulatorView.Draw(ScreenManager.SpriteBatch);
            }
            ScreenManager.SpriteBatch.End();
        }

        bool firstRun = true;

        public override void HandleInput(InputState input) {
            if (firstRun) {
                ScreenManager.AddScreen(new PauseScreen(GetTitle(), GetDetails()));
                firstRun = false;
            }
            
            if (input.PauseGame) {
                ScreenManager.AddScreen(new PauseScreen(GetTitle(), GetDetails()));
            }
            else {                
#if !XBOX
                HandleMouseInput(input);
#endif
            }
            base.HandleInput(input);
        }

        FixedLinearSpring mousePickSpring;
        Geom pickedGeom;
#if !XBOX
        private void HandleMouseInput(InputState input) {
            Vector2 point = new Vector2(input.CurrentMouseState.X, input.CurrentMouseState.Y);
            if (input.LastMouseState.LeftButton == ButtonState.Released && input.CurrentMouseState.LeftButton == ButtonState.Pressed) {
                clicked = true;
                //create mouse spring
                pickedGeom = physicsSimulator.Collide(point);
                if (pickedGeom != null) {
                    if ((pickedGeom.Body.IsHand == true) && (pickedGeom.Body.IsStatic == true))
                    {
                        pickedGeom.Body.IsStatic = false;
                        pickedGeom.CollisionEnabled = false;
                    }
                    mousePickSpring = ControllerFactory.Instance.CreateFixedLinearSpring(physicsSimulator, pickedGeom.Body, pickedGeom.Body.GetLocalPosition(point), point, 100,1);
                }
            }
            else if (input.LastMouseState.LeftButton == ButtonState.Pressed && input.CurrentMouseState.LeftButton == ButtonState.Released) {
                clicked = false;
                //destroy mouse spring
                if (mousePickSpring != null && mousePickSpring.IsDisposed == false) {
                    mousePickSpring.Dispose();
                    mousePickSpring = null;
                    if ((pickedGeom.Body.IsHand == true) && (pickedGeom.Body.IsStatic == false) && (pickedGeom.CollisionEnabled) == false)
                    {
                        pickedGeom.CollisionEnabled = true;
                    }
                }
            }
            //move anchor point
            if (input.CurrentMouseState.LeftButton == ButtonState.Pressed && mousePickSpring != null) {
                    mousePickSpring.WorldAttachPoint = point;
            }
        }
#endif

        public string GetTitle() {
            return "Two Bodies With Geom";  
        }

        public string GetDetails() {
            StringBuilder sb = new StringBuilder();
            /*
            sb.AppendLine("This demo shows two bodies each with a single geometry");
            sb.AppendLine("object attached.");
            sb.AppendLine("");
            sb.AppendLine("GamePad:");
            sb.AppendLine("  -Rotate : left and right triggers");
            sb.AppendLine("  -Move : left thumbstick");
            sb.AppendLine("");
            sb.AppendLine("Keyboard:");
            sb.AppendLine("  -Rotate : left and right arrows");
            sb.AppendLine("  -Move : A,S,D,W");
            sb.AppendLine("");
            sb.AppendLine("Mouse");
            sb.AppendLine("  -Hold down left button and drag");
            */
            return sb.ToString();
        }
    }
}
